package com.uml.youmath;

import java.util.HashMap;
import java.util.Random;

/**
 * This class is designed to provide questions and answers for the YouMath
 * Android application.
 * 
 * @author james.dalphond@gmail.com
 */
public class ProblemMaker {

    public static final int ADDITION = 0;
    public static final int SUBTRACTION = 1;
    public static final int MULTIPLICATION = 2;
    public static final int DIVISION = 3;
    public static final int NUMBER_IDENTITY = 4;
    
    public static final int EVEN = 0;
    public static final int ODD = 1;
    public static final int PRIME = 2;
    public static final int ANY = 3;
    
    public static final int QUESTION = 0;
    
    private long seed;
    private HashMap<Integer, String> problem;
    private Random generator;

    public ProblemMaker() {
        seed = System.currentTimeMillis();
    }

    /**
     * Returns a HashMap<Integers,String> of problems and answers
     * for regular arithmetic problems. 
     * 
     * Key 0 = Question
     * Keys 1-4 = Answers (1 is correct, 2-4 are false)
     * 
     * @param Subject 
     * @param Difficulty
     * @return
     */
    public HashMap<Integer, String> GetProblem(int Subject, int Difficulty) {
        switch (Subject) {
            case 0:
                return getAdditionProblem(Difficulty);
            case 1:
                return getSubtractionProblem(Difficulty);
            case 2:
                return getMultiplicationProblem(Difficulty);
            case 3:
                return getDivisionProblem(Difficulty);
            case 4:
            	return GetProblem(NUMBER_IDENTITY, ANY, Difficulty);
        }
        return null;
    }
    
    /**
     * Returns a HashMap<Integer,String> of problems and answers
     * for number identity questions
     * 
     * @param Subject
     * @param Type
     * @param Difficulty
     * @return
     */
    public HashMap<Integer, String> GetProblem(int Subject, int Type, int Difficulty ){
    	problem = new HashMap<Integer, String>();
    	
    	generator = new Random(seed +=1);
    	
    	int max, min, x;
    	
    	String question = "";
    	
    	if(Difficulty == 0){
    		min = 1;
    		max = 10;
    	} else if(Difficulty == 1){
    		min = 5;
    		max = 25;
    	} else if(Difficulty == 2){
    		min = 20;
    		max = 50;
    	} else if(Difficulty == 4){
    		min = 50;
    		max = 100;
    	} else {
    		min = 100;
    		max = 1000;
    	}
    	
    	x = generator.nextInt(max-min+1)+min;
    	
    	if(Type == ANY){
    		Type = generator.nextInt(2);
    	}
    	
    	switch(Type){
    		case EVEN:
    			question = "Is " + x + " even? ";
    			if(x%2==0){
    				problem.put(1,"True");
    				problem.put(2,"False");
    			} else {
    				problem.put(1,"False");
    				problem.put(2,"True");
    			}
    			break;
    		case ODD:
    			question = "Is " + x + " odd? ";
    			if(x%2==1){
    				problem.put(1,"True");
    				problem.put(2,"False");
    			} else {
    				problem.put(1,"False");
    				problem.put(2,"True");
    			}
    			break;
    		case PRIME:
    			if(isPrime(x)){
    				problem.put(1,"True");
    				problem.put(2,"False");
    			} else {
    				problem.put(1,"False");
    				problem.put(2,"True");
    			}
    			question = "Is " + x + " prime? ";
    			break;
    	}
    	
    	problem.put(QUESTION, question);
    	
    	return problem;
    	
    }
    
    /**
     * Get a question from a random subject at the specified
     * difficulty.
     * 
     * @param Difficulty
     * @return
     */
    public HashMap<Integer, String> getRandomProblem(int Difficulty){
    	generator = new Random(seed += 1);
    	int subject = generator.nextInt(5);
    	return GetProblem(subject,Difficulty);
    }
    

    private HashMap<Integer, String> getAdditionProblem(int Difficulty) {
        problem = new HashMap<Integer, String>();

        generator = new Random(seed += 1);
        int x, y, answer;
        int max, min;

        String question;

        min = 1;

        if (Difficulty == 0) {
            max = 5;
        } else if (Difficulty == 1) {
            max = 10;
        } else if (Difficulty == 2) {
            max = 20;
        } else {
            max = 100;
        }

        x = generator.nextInt(max - min + 1) + min;
        y = generator.nextInt((max - x) + 1) + min;

        answer = x + y;
        question = x + "+" + y + " = ?";

        //Put question in hashmap
        problem.put(QUESTION, question);
        //Put answer in hashmap
        problem.put(1, answer + "");

        //Put wrong answers in hashmap
        problem.put(2, (answer + 1) + "");
        problem.put(3, (answer + 2) + "");
        problem.put(4, (answer - 1) + "");

        return problem;
    }

    private HashMap<Integer, String> getSubtractionProblem(int Difficulty) {
        problem = new HashMap<Integer, String>();

        generator = new Random(seed += 1);
        int x, y, answer;
        int max, min;

        String question;

        min = 1;

        if (Difficulty == 0) {
            max = 5;
        } else if (Difficulty == 1) {
            max = 10;
        } else if (Difficulty == 2) {
            max = 20;
        } else {
            max = 100;
        }

        x = generator.nextInt(max - min + 1) + min;
        y = generator.nextInt(x - min + 1) + min;

        answer = x - y;
        question = x + "-" + y + " = ?";

        //Put question in hashmap
        problem.put(QUESTION, question);
        //Put answer in hashmap
        problem.put(1, answer + "");

        //Put wrong answers in hashmap
        problem.put(2, (answer + 1) + "");
        problem.put(3, (answer + 2) + "");
        if (answer - 1 > 0) {
            problem.put(4, (answer - 1) + "");
        } else {
            problem.put(4, answer + 3 + "");
        }

        return problem;
    }

    private HashMap<Integer, String> getMultiplicationProblem(int Difficulty) {
        problem = new HashMap<Integer, String>();

        generator = new Random(seed += 1);
        int x, y, answer;
        int max, min;

        String question;

        min = 1;
        max = 10;

        int which = generator.nextInt() % 2;
        if (which == 0) {
            x = Difficulty;
            y = generator.nextInt(max - min + 1) + 1;
        } else {
            x = generator.nextInt(max - min + 1) + 1;
            y = Difficulty;
        }

        question = x + "x" + y + " = ?";
        answer = x * y;

        //Put question in hashmap
        problem.put(QUESTION, question);
        //Put answer in hashmap
        problem.put(1, answer + "");

        //Put wrong answers in hashmap
        problem.put(2, (answer + 1) + "");
        problem.put(3, (answer + 2) + "");
        if (answer - 1 > 0) {
            problem.put(4, (answer - 1) + "");
        } else {
            problem.put(4, answer + 3 + "");
        }

        return problem;
    }

    private HashMap<Integer, String> getDivisionProblem(int Difficulty) {
        problem = new HashMap<Integer, String>();
        generator = new Random(seed += 1);
        int var, answer;
        int max, min;
        String question;


        if (Difficulty == 0) {
            //Divide by 1 (fact to 10)
            min = 1;
            max = 10;
        } else if (Difficulty == 1) {
            //Divide by 2 (fact to 20)
            min = 2;
            max = 20;
        } else if (Difficulty == 2) {
            //Divide by 5 (fact to 50)
            min = 5;
            max = 50;
        } else if (Difficulty == 3) {
            //Divide by 10 (fact to 100)
        	min = 3;
            max = 60;
        } else {
            //Divide by 3  (fact to 60)
            min = 3;
            max = 60;
        } 

        max = max / min;

        var = generator.nextInt(max - min + 1) + 1;
        
        question = (var * min) + " / " + min + " = ?";
        answer = var;

        problem.put(QUESTION,question);
        problem.put(1, answer+"");

        //Put wrong answers in hashmap
        problem.put(2, (answer + 1) + "");
        problem.put(3, (answer + 2) + "");
        if (answer - 1 > 0) {
            problem.put(4, (answer - 1) + "");
        } else {
            problem.put(4, answer + 3 + "");
        }

        return problem;
    }
    
    
   
    private boolean isPrime(int n) {
        //check if n is a multiple of 2
        if (n%2==0) return false;
        //if not, then just check the odds
        for(int i=3;i*i<=n;i+=2) {
            if(n%i==0)
                return false;
        }
        return true;
    }
    
}
